package com.it;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ThreadDemo extends Thread {
    // 定义一个集合，用来存储礼物
    private List<String> gift;
    //用来存储每个人发送的礼物数量
    private int count;

    // 有参构造方法（传递集合和线程名）
    public ThreadDemo(List<String> gifts, String name) {
        super(name);
        this.gift = gifts;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    // 重写run方法
    @Override
    public void run() {
        //获取当前线程的名字
        String name = Thread.currentThread().getName();
        // 循环发送礼物
        Random random = new Random();
        while (true) {
            // 同步代码块
            synchronized (gift) {
                if (gift.size() <= 10) {
                    break;
                }
                String remove = gift.remove(random.nextInt(gift.size()));
                System.out.println(name + "送出" + remove);
                count++;
            }
        }
    }
}
